package com.jinly.ryder.manage.web;

import com.jinly.ryder.account.web.utils.AuthUtil;
import com.jinly.ryder.common.auth.annotation.Auth;
import com.jinly.ryder.common.auth.annotation.AuthParam;
import com.jinly.ryder.common.auth.model.AuthRole;
import com.jinly.ryder.common.auth.model.AuthType;
import com.jinly.ryder.manage.service.DeployGroupService;
import com.jinly.ryder.manage.service.model.DeployGroup;
import com.jinly.ryder.manage.service.model.DeployStatus;
import com.jinly.ryder.manage.web.mapping.DeployGroupVOMapping;
import com.jinly.ryder.manage.web.vo.DeployGroupVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("manage/deployGroup")
@RequiredArgsConstructor
public class DeployGroupController {

    private final DeployGroupVOMapping deployGroupVOMapping;
    private final DeployGroupService deployGroupService;

    @PostMapping("save")
    @Auth(type = AuthType.APP, role = AuthRole.ADMIN, value = "#{appName}")
    public void saveDeployGroup(@RequestBody @AuthParam("appName") DeployGroupVO deployGroupVO) {
        deployGroupService.save(deployGroupVOMapping.toModel(deployGroupVO));
    }

    @DeleteMapping("{deployGroupId}")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "app")
    public void deleteGroup(@PathVariable Integer deployGroupId) {
        deployGroupService.deleteGroup(deployGroupId);
    }

    @PostMapping("saveProp")
    public void saveDeployGroupProp(@RequestBody DeployGroupVO deployGroupVO) {
        DeployGroup deployGroup = deployGroupVOMapping.toModel(deployGroupVO);
        DeployGroup.DeployGroupProp props = deployGroup.getProps();
        if (props != null && props.getJavaOpts() != null) {
            props.setJavaOpts(props.getJavaOpts().replaceAll("\n", " "));
        }
        DeployGroup updateDeployGroup = deployGroupService.get(deployGroupVO.getId());
        if (updateDeployGroup != null) {
            AuthUtil.check(AuthType.APP, updateDeployGroup.getAppName(), AuthRole.DEVELOPER);
            updateDeployGroup.setProps(deployGroup.getProps());
            deployGroupService.save(updateDeployGroup);
        }
    }

    @PutMapping("closeDeploy/{deployGroupId}")
    public void closeDeploy(@PathVariable Integer deployGroupId) {
        DeployGroup deployGroup = deployGroupService.get(deployGroupId);
        AuthUtil.check(AuthType.APP, deployGroup.getAppName(), AuthRole.DEVELOPER);
        if (deployGroup.getDeployStatus() == DeployStatus.DEPLOYING) {
            deployGroupService.changeDeployStatus(deployGroupId, DeployStatus.NONE, null);
        }
    }

    @GetMapping("{deployGroupId}")
    public DeployGroupVO getDeployGroup(@PathVariable Integer deployGroupId) {
        DeployGroup deployGroup = deployGroupService.get(deployGroupId);
        AuthUtil.check(AuthType.APP, deployGroup.getAppName(), AuthRole.DEVELOPER);
        return deployGroupVOMapping.toVo(deployGroup);
    }

    @GetMapping("list")
    @Auth(type = AuthType.APP, role = AuthRole.DEVELOPER, value = "#{appName}")
    public List<DeployGroupVO> groupList(@AuthParam("appName") String appName) {
        return deployGroupVOMapping.toVoList(deployGroupService.list(appName));
    }
}
